package service

import (
	"e_mall_server/dao/mysql"
	"e_mall_server/models"
	"fmt"
)

/**
 * @Author: zhaobin
 * @Description TODO
 * @Date: 2022-05-10 15:49
 */

// GetGoodsList 获取商品列表接口
func GetGoodsList(pageParam *models.PageParam) (goodsResponse models.GoodsResponse, err error) {
	// 对查询条件加工
	pageParam.Query = "%" + pageParam.Query + "%"

	// 查询总数量
	goodsResponse.Total = mysql.GetGoodsCount(pageParam)
	// 查询商品列表
	goodsResponse.GoodsList, err = mysql.GetGoodsList(pageParam)
	if err != nil {
		return goodsResponse, err
	}
	return
}

// GetGoodsListByQuery 获取商品列表接口
func GetGoodsListByQuery(goodsQueryParam *models.GoodsQueryParam) (goodsResponse models.GoodsResponse, err error) {

	query := make(map[string]interface{})

	var tempSlice []int64
	// 1.查询分类信息，获取该分类下所有的子分类
	if goodsQueryParam.CateId != 0 {
		// 当不是顶级分类的时候查询
		goodsCate, _ := mysql.GetGoodsCateDetailById(goodsQueryParam.CateId)
		if goodsCate.ParentId == 0 { //顶级分类
			//获取顶级分类下面的二级分类
			goodsCateList := mysql.GetGoodsCateListByParentId(goodsCate.Id)
			for i := 0; i < len(goodsCateList); i++ {
				tempSlice = append(tempSlice, goodsCateList[i].Id)
			}
		} else {
			tempSlice = append(tempSlice, goodsQueryParam.CateId)
		}
	}

	// 2.如果不是顶级模块查询时加入所有子分类id
	if goodsQueryParam.CateId != 0 {
		query["cate_id"] = tempSlice
	}
	// 关于新品、热卖、精品
	switch goodsQueryParam.GoodsType {
	case "hot":
		query["is_hot"] = 1
	case "best":
		query["is_best"] = 1
	case "new":
		query["is_new"] = 1
	default:
		break
	}

	// 对查询条件加工
	goodsQueryParam.Query = "%" + goodsQueryParam.Query + "%"
	//zap.L().Debug("查询条件", zap.Any("value", query))
	// 通过map查询商品信息
	goodsList, err := mysql.GetGoodsByMap(query, goodsQueryParam)
	goodsResponse.GoodsList = goodsList
	// 查询商品总数量
	count := mysql.GetGoodsCountByMap(query, goodsQueryParam)
	goodsResponse.Total = count
	return goodsResponse, err
}

// AddGoods 添加商品信息
func AddGoods(goodsParam *models.GoodsParam) error {

	// 1.添加商品数据
	goods := models.Goods{
		Title:        goodsParam.Title,
		SubTitle:     goodsParam.SubTitle,
		CateId:       goodsParam.CateId,
		ClickCount:   goodsParam.ClickCount,
		GoodsNumber:  goodsParam.GoodsNumber,
		Price:        goodsParam.Price,
		MarketPrice:  goodsParam.MarketPrice,
		GoodsImg:     goodsParam.GoodsImg,
		GoodsColor:   goodsParam.GoodsColor,
		GoodsContent: goodsParam.GoodsContent,
		IsHot:        goodsParam.IsHot,
		IsBest:       goodsParam.IsBest,
		IsNew:        goodsParam.IsNew,
		Sort:         goodsParam.Sort,
		Status:       goodsParam.Status,
	}

	if err := mysql.AddGoods(&goods); err != nil {
		return err
	}
	fmt.Println(goods)
	// 使用协程
	// 2.添加参数信息
	for _, v := range goodsParam.GoodsAttrList {
		goodsAttr := models.GoodsAttr{
			GoodsId: goods.Id,
			CateId:  v.CateId,
			AttrId:  v.Id,
			Title:   v.Title,
			Type:    v.Type,
			Value:   v.Value,
			Sort:    v.Sort,
			Status:  v.Status,
		}
		if err := mysql.AddGoodsAttr(&goodsAttr); err != nil {
			return err
		}
	}

	// 3.添加图片信息
	for _, v := range goodsParam.GoodsImageList {
		goodsImage := models.GoodsImage{}
		goodsImage.GoodsId = goods.Id
		goodsImage.ImgUrl = v
		goodsImage.Sort = 100
		goodsImage.Status = 1
		if err := mysql.AddGoodsImage(&goodsImage); err != nil {
			return err
		}
	}

	return nil
}

// UpdateGoods 修改商品信息
func UpdateGoods(Goods *models.Goods) int64 {
	return mysql.UpdateGoods(Goods)
}

// DeleteGoodsById 通过商品id删除商品数据
func DeleteGoodsById(id int64) int64 {
	return mysql.DeleteGoodsById(id)
}

// GetGoodsDetailById 通过商品id获取商品详细信息
func GetGoodsDetailById(id int64) (models.Goods, error) {
	return mysql.GetGoodsDetailById(id)
}
